package control;

import java.math.BigDecimal;
import java.sql.Time;
import java.util.Comparator;

import data.Constant;
import data.TimeStock;

public class TimeStockCompare implements Comparator<TimeStock> {

	private int col;
	
	public TimeStockCompare(int column){
		col = column;
	}
	
	@Override
	public int compare(TimeStock arg0, TimeStock arg1) {
		if(arg0.getCode().equals("sz399001")||arg0.getCode().equals("sh000001")
				||arg1.getCode().equals("sz399001")||arg1.getCode().equals("sh000001")){
			return 0;
		}
		switch (col) {
		case 0:
			return arg1.getCode().compareTo(arg0.getCode());
		case 1:
			return arg0.getStockName().compareTo(arg1.getStockName());
		case 2:
			return arg0.getCurPrice().compareTo(arg1.getCurPrice());
		case 4:
			return arg1.getRatePercent().compareTo(arg0.getRatePercent());
		case 6:
			BigDecimal amountBat1 = Constant.ZERO;
			if(arg1.getLastStock().getAmount().compareTo(Constant.ZERO)!=0){
				amountBat1 = arg1.getAmount().divide(arg1.getLastStock().getAmount(), 2,	BigDecimal.ROUND_HALF_UP);
			}
			BigDecimal amountBat0 = Constant.ZERO;
			if(arg0.getLastStock().getAmount().compareTo(Constant.ZERO)!=0){
				amountBat0 = arg0.getAmount().divide(arg0.getLastStock().getAmount(), 2,	BigDecimal.ROUND_HALF_UP);
			}
			return amountBat1.compareTo(amountBat0);
		case 10:
			return arg0.getIndustry().compareTo(arg1.getIndustry());
		case 11:
			return 0;
		case 12:
			return 0;
		case 13:
			return arg1.getPriceWive().compareTo(arg0.getPriceWive());
		case 14:
			Time t0 = arg0.getSelectTime();
			Time t1 = arg1.getSelectTime();
			if(t0==null){
				t0 = new Time(0);
			}
			if(t1==null){
				t1 = new Time(0);
			}
			return t1.compareTo(t0);
		case 16:
			BigDecimal benifit0 = Constant.ZERO;
			if(arg0.getSelectPrice()!=null){
				benifit0 = arg0.getCurPrice().subtract(arg0.getSelectPrice()).divide(arg0.getPreDayPrice(),4,BigDecimal.ROUND_HALF_UP);
			}
			BigDecimal benifit1 = Constant.ZERO;
			if(arg1.getSelectPrice()!=null){
				benifit1 = arg1.getCurPrice().subtract(arg1.getSelectPrice()).divide(arg1.getPreDayPrice(),4,BigDecimal.ROUND_HALF_UP);
			}
			return benifit1.compareTo(benifit0);
		case 17:
			Time t3 = arg0.getAmountInrecTime();
			Time t4 = arg1.getAmountInrecTime();
			if(t3==null){
				t3 = new Time(0);
			}
			if(t4==null){
				t4 = new Time(0);
			}
			return t4.compareTo(t3);
		case 18:
			return arg1.getQtyBat()-arg0.getQtyBat();
		default:
			break;
		}
		return 0;
	}

}
